import { getManager, managerCount } from "../../utils/request"

const state = {
    manager:[],
    size:2, //每页显示的页数
    page:1,//当前的页码数
    count:0// 总记录数
}
const mutations = {
     changeManager(state,arr){
         state.manager=arr
     },
     changeCount(state,count){
         state.count = count
     },
     changePage(state,page){
         state.page=page
     }
}
const actions ={
    managerActions(context){
        var params = {
            size:context.state.size,
            page:context.state.page
        }
         
        getManager(params).then(res=>{
            // 判断当前页码数是否为空
            if((res.data.list ===null || res.data.list.length ===0) && context.state.page >1){
                var page = context.state.page-1
                context.commit('changePage',page)
                context.dispatch('managerActions')
                return
            }
            context.commit('changeManager',res.data.list)
        })
        
    },
    // 获取总记录数
    countActions(context){
        managerCount().then(res=>{
            context.commit('changeCount',res.data.list[0].total)
        })
    },

    // 获取当前页实现跳转
    pageActions(context,page){
        context.commit('changePage',page)
    }
}
const getters ={
    manager(state){
        return state.manager
    },
    count(state){
        return state.count
    },
    size(state){
        return state.size
    },
}


export default {
    state,
    mutations,
    actions,
    getters,
    namespaced:true
}